package com.airlines.crewassignment.service.crew.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.apache.commons.lang3.StringUtils;
import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.airlines.base.config.response.ResponseResult;
import com.airlines.base.config.response.ResponseResultBuilder;
import com.airlines.common.DateTimeRangeUtil;
import com.airlines.common.ListSortUtil;
import com.airlines.crewassignment.constant.CrewTaskConstant;
import com.airlines.crewassignment.dao.CrewMapper;
import com.airlines.crewassignment.dao.CrewTaskMapper;
import com.airlines.crewassignment.domain.Crew;
import com.airlines.crewassignment.domain.CrewTask;
import com.airlines.crewassignment.domain.DateTimeRange;
import com.airlines.crewassignment.entity.crew.CrewSchedule;
import com.airlines.crewassignment.service.crew.CrewTaskService;

/**
 * @author xxxx
 * @date 2018年4月27日 
 * @description
 */
@Service
public class CrewTaskServiceImpl implements CrewTaskService{
	@Autowired
	private CrewTaskMapper crewTaskMapper;
	@Autowired
	private CrewMapper crewMapper;
	@Override
	public List<CrewTask> listCrewTask(DateTimeRange dateTimeRange,String dutyCode, String workcode) {
		List<CrewTask> crewTaskList = new ArrayList<>();
		//1.查询所有机组的飞行任务
		List<CrewTask> crewTaskForFlightList = this.listCrewTaskForFlight(dateTimeRange,dutyCode, workcode);
		crewTaskList.addAll(crewTaskForFlightList);
		
		//2.查询加机组任务（非因私）
		List<CrewTask> crewTaskForAddCrewList = this.listCrewTaskForAddCrew(dateTimeRange,dutyCode, workcode);
		crewTaskList.addAll(crewTaskForAddCrewList);
		
		//3.查询所有值班任务
		List<CrewTask> crewTaskForDuty = this.listCrewTaskForDuty(dateTimeRange,dutyCode, workcode);
		crewTaskList.addAll(crewTaskForDuty);
		
		//4.备份任务
		List<CrewTask> crewTaskForBak = this.listCrewTaskForBak(dateTimeRange,dutyCode, workcode);
		crewTaskList.addAll(crewTaskForBak);
		
		//5.不能飞（训练）信息
		List<CrewTask> crewTaskForTrain = this.listCrewTaskForTrain(dateTimeRange,dutyCode, workcode);
		crewTaskList.addAll(crewTaskForTrain);
		
		//99.初始化时间段
		for(CrewTask ct : crewTaskList){
			DateTimeRange dr = new DateTimeRange(ct.getStart(),ct.getEnd());
			ct.setDateTimeRange(dr);
		}
		
		//排一下序号
		ListSortUtil.sort(crewTaskList, true, "workcode","start");
		return crewTaskList;
	}
	
	@Override
	public List<CrewTask> listCrewTaskAll(DateTimeRange dateTimeRange,String dutyCode,  String workcode) {
		List<CrewTask> crewTaskList = new ArrayList<>();
		//1.查询所有机组的飞行任务
		List<CrewTask> crewTaskForFlightList = this.listCrewTaskForFlight(dateTimeRange,dutyCode, workcode);
		crewTaskList.addAll(crewTaskForFlightList);
		
		//2.查询加机组任务
		List<CrewTask> crewTaskForAddCrewAllList = this.listCrewTaskForAddCrewAll(dateTimeRange,dutyCode, workcode);
		crewTaskList.addAll(crewTaskForAddCrewAllList);
		
		//3.查询所有值班任务
		List<CrewTask> crewTaskForDuty = this.listCrewTaskForDuty(dateTimeRange,dutyCode,workcode);
		crewTaskList.addAll(crewTaskForDuty);
		
		//4.备份任务
		List<CrewTask> crewTaskForBak = this.listCrewTaskForBak(dateTimeRange, dutyCode,workcode);
		crewTaskList.addAll(crewTaskForBak);
		
		//5.不能飞(所有)信息
		List<CrewTask> crewTaskForCannotFly = this.listCrewTaskForCannotFly(dateTimeRange,dutyCode, workcode);
		crewTaskList.addAll(crewTaskForCannotFly);
		
		//6.生日信息
		List<CrewTask> crewTaskForBirthday = this.listCrewTaskForBirthday(dateTimeRange,dutyCode, workcode);
		crewTaskList.addAll(crewTaskForBirthday);
		
		//99.初始化时间段
		for(CrewTask ct : crewTaskList){
			DateTimeRange dr = new DateTimeRange(ct.getStart(),ct.getEnd());
			ct.setDateTimeRange(dr);
		}
		
		//排一下序号
		ListSortUtil.sort(crewTaskList, true, "workcode","start");
		return crewTaskList;
	}

	

	@Override
	public List<CrewTask> listCrewTaskForFlight(DateTimeRange dateTimeRange,String dutyCode,  String workcode) {
		List<CrewTask> list = crewTaskMapper.listCrewTaskForFlight(dateTimeRange, dutyCode, workcode);
		for(CrewTask ct : list){
			ct.setTaskType(CrewTaskConstant.TASK_TYPE_FLIGHT);
			ct.setTaskName(CrewTaskConstant.TASK_TYPE_FLIGHT_NAME);
		}
		return list;
	}

	@Override
	public List<CrewTask> listCrewTaskForAddCrew(DateTimeRange dateTimeRange,String dutyCode,  String workcode) {
		List<CrewTask> list = crewTaskMapper.listCrewTaskForAddCrew(dateTimeRange, dutyCode, workcode);
		for(CrewTask ct : list){
			ct.setTaskType(CrewTaskConstant.TASK_TYPE_ADDCREW);
			ct.setTaskName(CrewTaskConstant.TASK_TYPE_ADDCREW_NAME);
		}
		return list;
	}

	@Override
	public List<CrewTask> listCrewTaskForDuty(DateTimeRange dateTimeRange,String dutyCode,  String workcode) {
		List<CrewTask> list = crewTaskMapper.listCrewTaskForDuty(dateTimeRange, dutyCode, workcode);
		for(CrewTask ct : list){
			ct.setTaskType(CrewTaskConstant.TASK_TYPE_DUTY);
			ct.setTaskName(CrewTaskConstant.TASK_TYPE_DUTY_NAME);
		}
		return list;
	}

	@Override
	public List<CrewTask> listCrewTaskForBak(DateTimeRange dateTimeRange,String dutyCode,  String workcode) {
		List<CrewTask> list = crewTaskMapper.listCrewTaskForBak(dateTimeRange, dutyCode, workcode);
		for(CrewTask ct : list){
			ct.setTaskType(CrewTaskConstant.TASK_TYPE_BAK);
			ct.setTaskName(CrewTaskConstant.TASK_TYPE_BAK_NAME);
		}
		return list;
	}
	@Override
	public List<CrewTask> listCrewTaskForTrain(DateTimeRange dateTimeRange,String dutyCode,  String workcode) {
		List<CrewTask> list = crewTaskMapper.listCrewTaskForTrain(dateTimeRange, dutyCode, workcode);
		for(CrewTask ct : list){
			ct.setTaskType(CrewTaskConstant.TASK_TYPE_TRAIN);
			ct.setTaskName(CrewTaskConstant.TASK_TYPE_TRAIN_NAME);
		}
		return list;
	}

	@Override
	public List<CrewTask> listCrewTaskForAddCrewAll(DateTimeRange dateTimeRange,String dutyCode,  String workcode) {
		List<CrewTask> list = crewTaskMapper.listCrewTaskForAddCrewAll(dateTimeRange, dutyCode, workcode);
		for(CrewTask ct : list){
			ct.setTaskType(CrewTaskConstant.TASK_TYPE_ADDCREW_ALL);
			ct.setTaskName(CrewTaskConstant.TASK_TYPE_ADDCREW_ALL_NAME);
		}
		return list;
	}

	@Override
	public List<CrewTask> listCrewTaskForCannotFly(DateTimeRange dateTimeRange,String dutyCode,  String workcode) {
		List<CrewTask> list = crewTaskMapper.listCrewTaskForTrain(dateTimeRange, dutyCode, workcode);
		for(CrewTask ct : list){
			ct.setTaskType(CrewTaskConstant.TASK_TYPE_CANNOT_FLY);
			ct.setTaskName(CrewTaskConstant.TASK_TYPE_CANNOT_NAME);
		}
		return list;
	}
	
	@Override
	public List<CrewTask> listCrewTaskForBirthday(DateTimeRange dateTimeRange, String dutyCode, String workcode) {
		List<Crew> crewList = crewMapper.listCrewBase(dutyCode,workcode);
		List<CrewTask> taskList = new ArrayList<>();
		for(Crew crew: crewList){
			Date birthday = crew.getBirthday();
			if(birthday !=null){
				DateTimeRange birthdayRange = DateTimeRangeUtil.getToday(birthday);
				if(DateTimeRangeUtil.isIntersection(dateTimeRange, birthdayRange)){
					CrewTask ct = new CrewTask();
					ct.setWorkcode(crew.getWorkcode());
					ct.setStart(birthdayRange.getStart());
					ct.setEnd(birthdayRange.getEnd());
					ct.setTaskType(CrewTaskConstant.TASK_TYPE_BIRTHDAY);
					ct.setTaskName(CrewTaskConstant.TASK_TYPE_BIRTHDAY_NAME);
					ct.setTaskDesc(CrewTaskConstant.TASK_TYPE_BIRTHDAY_NAME);
					taskList.add(ct);
				}
			}
			
		}
		return taskList;
	}

	@Override
	public List<CrewSchedule> listCrewSchedule(DateTimeRange dateTimeRange, String dutyCode, String workcode) {
		List<CrewSchedule> csList = new ArrayList<>();
		
		List<CrewTask> taskList = this.listCrewTaskAll(dateTimeRange, dutyCode, workcode);
		//1.查询所有人员工号
		Set<String> workcodeSet = new HashSet<>();
		for(CrewTask ct : taskList){
			workcodeSet.add(ct.getWorkcode());
		}
		
		//2.构造每人每天的日历信息
		List<DateTime> dateTimeList = DateTimeRangeUtil.listDateTime(dateTimeRange);
		for(DateTime dateTime : dateTimeList){		
			DateTimeRange everyDay = new DateTimeRange(dateTime,dateTime.millisOfDay().withMaximumValue());
			for(CrewTask ct : taskList){
				//判断任务与每天的日历是否有交集，如果有交集，则填充本员工本天的日历信息
				DateTimeRange taskDateTimeRange = ct.getDateTimeRange();
				boolean isIntersection = DateTimeRangeUtil.isIntersection(everyDay, taskDateTimeRange);
				if(isIntersection){
					CrewSchedule cs = this.getCrewSchedule(ct.getWorkcode(), dateTime.toDate(), csList);
					if(cs == null){
						cs  = new CrewSchedule();
						cs.setWorkcode(ct.getWorkcode());
						cs.setDate(dateTime.toDate());
						csList.add(cs);
					}					
					cs.getCrewTaskList().add(ct);
					
				}
			}
		}
		
		//3.构造每人每天日历说明信息
		for(CrewSchedule cs : csList){
			StringBuffer sb = new StringBuffer();
			sb.append("");
			for(CrewTask ct :cs.getCrewTaskList()){
				sb.append(ct.getTaskDesc());
				sb.append("\r\n");
			}
			cs.setScheduleContent(sb.toString());
			cs.setCrewTaskListSize(cs.getCrewTaskList().size());
		}
		return csList;
	}

	private CrewSchedule getCrewSchedule(String workcode,Date date,List<CrewSchedule> csList){
		for(CrewSchedule c : csList){
			if(StringUtils.equals(c.getWorkcode(), workcode) && date.equals(c.getDate())){
				return c;
			}
		}
		return null;
	}

	@Override
	public List<CrewTask> listCrewTaskForLastSickLeave(String dutyCode, String workcode, Date beforeFlightDate) {
		List<CrewTask> list = crewTaskMapper.listCrewTaskForLastSickLeave(dutyCode, workcode, beforeFlightDate);
		for(CrewTask ct : list){
			ct.setTaskType(CrewTaskConstant.TASK_TYPE_SICK_LEAVE);
			ct.setTaskName(CrewTaskConstant.TASK_TYPE_SICK_LEAVE_NAME);
		}
		return list;
	}

	@Override
	public List<CrewTask> listCrewTaskPage(DateTimeRange dateTimeRange, String dutyCode, String[] workcodeArray) {
		List<CrewTask>  list = new ArrayList<>();
		if(workcodeArray == null){
			return null;
		}
		for(String workcode : workcodeArray){
			List<CrewTask> taskList = this.listCrewTaskAll(dateTimeRange, dutyCode, workcode);
			list.addAll(taskList);
		}
		return list;
	}

	@Override
	public ResponseResult<String> saveCrewTaskAssignmentCk(String workcode, Date start, Date end) {
		// TODO Auto-generated method stub
		return ResponseResultBuilder.successResult(null, null);
	}

	@Override
	public List<CrewTask> listCrewTaskForAssignment(String dutyCode, String workcode, DateTimeRange dateTimeRange) {
		List<CrewTask> crewTaskList = new ArrayList<>();
	
		
		//2.查询加机组任务（非因私）
		List<CrewTask> crewTaskForAddCrewList = this.listCrewTaskForAddCrew(dateTimeRange,dutyCode, workcode);
		crewTaskList.addAll(crewTaskForAddCrewList);
		
		//3.查询所有值班任务
		List<CrewTask> crewTaskForDuty = this.listCrewTaskForDuty(dateTimeRange,dutyCode, workcode);
		crewTaskList.addAll(crewTaskForDuty);
		
		//4.备份任务
		List<CrewTask> crewTaskForBak = this.listCrewTaskForBak(dateTimeRange,dutyCode, workcode);
		crewTaskList.addAll(crewTaskForBak);
		
		//5.不能飞（训练）信息
		List<CrewTask> crewTaskForTrain = this.listCrewTaskForTrain(dateTimeRange,dutyCode, workcode);
		crewTaskList.addAll(crewTaskForTrain);
		
		//6.任务安排
		List<CrewTask> assigmentList = crewTaskMapper.listCrewTaskForAssignment(dutyCode, workcode, dateTimeRange);
		crewTaskList.addAll(assigmentList);
		
		//99.初始化时间段
		for(CrewTask ct : crewTaskList){
			DateTimeRange dr = new DateTimeRange(ct.getStart(),ct.getEnd());
			ct.setDateTimeRange(dr);
		}
		
		//排一下序号
		ListSortUtil.sort(crewTaskList, true, "workcode","start");
		return crewTaskList;
	}	
}
